package com.example.android_demo01.activity;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.android_demo01.R;

import java.util.Calendar;

public class DatePickDialogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_date_pick_dialog);
    }


    /**
     * 日期控件
     * @param v
     */
    public void btnClick(View v){
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(this,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth) {
                         String text = "新选择了："+ year + "年" + month + "月" + dayOfMonth + "日";
                        Toast.makeText(DatePickDialogActivity.this, text, Toast.LENGTH_SHORT).show();
                    }
                }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    /**
     * 时间控件
     * @param v
     */
    public void btnClick2(View v){
        Calendar calendar = Calendar.getInstance();
        new TimePickerDialog(this,
                new TimePickerDialog.OnTimeSetListener(){
                    @Override
                    public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
                        String text = "新选择了："+hourOfDay + "时" + minute + "分";
                        Toast.makeText(DatePickDialogActivity.this, text, Toast.LENGTH_SHORT).show();
                    }
                }
                // 设置初始时间
                ,calendar.get(Calendar.HOUR_OF_DAY)
                ,calendar.get(Calendar.MINUTE)
                // true表示采用24小时时差
                ,true).show();
    }


    Handler handler = new Handler(){
        // 主线程（UI）线程
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == 0){
                pd.dismiss();
            }else if(msg.what == 1){
                pd.setProgress(msg.arg1);
            }
        }
    };

    /**
     * 进度条控件
     * @param v
     */
    public void btnClick3(View v){
        showProgress();
        // 新建一个子线程
        new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) { // 耗时
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message message = Message.obtain();
                    message.arg1 = i;
                    message.what = 1;
                    handler.sendMessage(message);
                }
                handler.sendEmptyMessage(0);
            }
        }.start();
    }

    ProgressDialog pd;
    public  void showProgress(){
        pd = new ProgressDialog(this);
        pd.setTitle("任务执行中...");
        pd.setMessage("请稍后...");
        pd.setCancelable(true);
        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pd.setMax(10);
        pd.show();

    }


    private long mExitTime;

    /**
     * 点击两次回退退出
     */
    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - mExitTime > 2000){
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        }else{
            finish();
        }
    }
}
